package com.taobao.txc.sample;

import com.taobao.txc.client.aop.annotation.TxcTransaction;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicLong;
import javax.sql.DataSource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

public class StandardModeClient2
{
  @TxcTransaction(timeout=180000)
  public void insert(AtomicLong paramAtomicLong, JdbcTemplate paramJdbcTemplate1, JdbcTemplate paramJdbcTemplate2)
  {
    long l = paramAtomicLong.getAndIncrement();
    paramJdbcTemplate1.update("insert into students (id,name,value) values (?,'abc', 100)", new Object[] { Long.valueOf(l) });
    paramJdbcTemplate2.update("insert into `order` (id,name,value) values (?,'abc', 100)", new Object[] { Long.valueOf(l) });
    if (paramAtomicLong.longValue() % 99L == 0L) {
      throw new RuntimeException("ccccccccc");
    }
  }
  
  @TxcTransaction(timeout=180000)
  public void update(AtomicLong paramAtomicLong, JdbcTemplate paramJdbcTemplate1, JdbcTemplate paramJdbcTemplate2)
  {
    long l = paramAtomicLong.getAndIncrement();
    Connection localConnection1 = null;
    Connection localConnection2 = null;
    Statement localStatement1 = null;
    Statement localStatement2 = null;
    try
    {
      localConnection1 = paramJdbcTemplate1.getDataSource().getConnection();
      localConnection1.setAutoCommit(false);
      localStatement1 = localConnection1.createStatement();
      localStatement1.execute("update students set value=200 where id=" + l);
      localStatement1.execute("update persons set score=score+1 where id=" + l % 1000L + " or id=" + (l + 1L) % 1000L);
      localConnection1.commit();
      localConnection1.setAutoCommit(true);
      if (paramAtomicLong.longValue() % 246L == 0L) {
        throw new RuntimeException("aaaaaaaaaaaaa");
      }
      localConnection2 = paramJdbcTemplate2.getDataSource().getConnection();
      localConnection2.setAutoCommit(false);
      localStatement2 = localConnection2.createStatement();
      localStatement2.execute("update `order` set `value`=300 where `id`=" + l);
      localStatement2.execute("update persons set score=score+1 where id=" + l % 1000L + " or id=" + (l + 1L) % 1000L);
      localConnection2.commit();
      localConnection2.setAutoCommit(true);
    }
    finally
    {
      if (localStatement1 != null) {
        localStatement1.close();
      }
      if (localConnection1 != null) {
        localConnection1.close();
      }
      if (localStatement2 != null) {
        localStatement2.close();
      }
      if (localConnection2 != null) {
        localConnection2.close();
      }
    }
    if (paramAtomicLong.longValue() % 123L == 0L) {
      throw new RuntimeException("bbbbbb");
    }
  }
  
  @TxcTransaction(timeout=180000)
  public void delete(AtomicLong paramAtomicLong, JdbcTemplate paramJdbcTemplate1, JdbcTemplate paramJdbcTemplate2)
  {
    long l = paramAtomicLong.getAndIncrement();
    paramJdbcTemplate1.update("delete from students where value > 100 and id=" + l);
    if (paramAtomicLong.longValue() % 112L == 0L) {
      throw new RuntimeException("eeeee");
    }
    paramJdbcTemplate2.update("delete from `order` where `value` > 100 and `id`=" + l);
    if (paramAtomicLong.longValue() % 111L == 0L) {
      throw new RuntimeException("fffff");
    }
  }
  
  public static void main(String[] paramArrayOfString)
  {
    String str = "standard-mode-client-context.xml";
    if ((paramArrayOfString.length > 0) && (paramArrayOfString[0].compareTo("-c") == 0))
    {
      str = "standard-mode-client-cobar-context.xml";
      System.out.println("cobar mode...\n\n");
    }
    else
    {
      System.out.println("tddl mode... \n\n");
    }
    ClassPathXmlApplicationContext localClassPathXmlApplicationContext = new ClassPathXmlApplicationContext(str);
    int i = 20;
    CountDownLatch localCountDownLatch = new CountDownLatch(i);
    StandardModeClient2 localStandardModeClient2 = (StandardModeClient2)localClassPathXmlApplicationContext.getBean("clientTest2");
    JdbcTemplate localJdbcTemplate1 = (JdbcTemplate)localClassPathXmlApplicationContext.getBean("jdbcTemplate1");
    JdbcTemplate localJdbcTemplate2 = (JdbcTemplate)localClassPathXmlApplicationContext.getBean("jdbcTemplate2");
    localJdbcTemplate1.execute("truncate  students");
    localJdbcTemplate1.execute("truncate  students_0001");
    localJdbcTemplate1.execute("truncate  students_0002");
    localJdbcTemplate2.execute("delete from  `order`");
    localJdbcTemplate1.execute("update persons set score=0");
    localJdbcTemplate2.execute("update persons set score=0");
    for (int j = 0; j < i; j++)
    {
      int k = j;
      localObject1 = new a(k, localStandardModeClient2, localJdbcTemplate1, localJdbcTemplate2, localCountDownLatch);
      ((Thread)localObject1).start();
    }
    try
    {
      localCountDownLatch.await();
    }
    catch (InterruptedException localInterruptedException1)
    {
      localInterruptedException1.printStackTrace();
    }
    System.out.println("at mode txc finish -----------------");
    System.out.println("wait for 3 minute  ------------------");
    try
    {
      Thread.sleep(180000L);
    }
    catch (InterruptedException localInterruptedException2)
    {
      localInterruptedException2.printStackTrace();
    }
    System.out.println("checking result --------------------");
    ArrayList localArrayList1 = new ArrayList();
    ArrayList localArrayList2 = new ArrayList();
    Object localObject1 = localJdbcTemplate1.queryForList("select id from students where value>100 order by id");
    List localList = localJdbcTemplate2.queryForList("select id from `order` where value>100 order by id");
    Object localObject2 = ((List)localObject1).iterator();
    Object localObject4;
    while (((Iterator)localObject2).hasNext())
    {
      localObject3 = (Map)((Iterator)localObject2).next();
      localIterator1 = ((Map)localObject3).values().iterator();
      while (localIterator1.hasNext())
      {
        localObject4 = localIterator1.next();
        localArrayList1.add(localObject4.toString());
      }
    }
    localObject2 = localList.iterator();
    while (((Iterator)localObject2).hasNext())
    {
      localObject3 = (Map)((Iterator)localObject2).next();
      localIterator1 = ((Map)localObject3).values().iterator();
      while (localIterator1.hasNext())
      {
        localObject4 = localIterator1.next();
        localArrayList2.add(localObject4.toString());
      }
    }
    System.out.println("id list on datasource1 which delete is rollbacked,size:" + localArrayList1.size() + ", value :" + localArrayList1);
    System.out.println("id list on datasource2 which delete is rollbacked,size:" + localArrayList2.size() + ", value :" + localArrayList2);
    if ((localArrayList1.size() == localArrayList2.size()) && (localArrayList1.containsAll(localArrayList2)) && (localArrayList2.containsAll(localArrayList1)))
    {
      System.out.println("the result is good.");
    }
    else
    {
      if (!localArrayList2.containsAll(localArrayList1))
      {
        localArrayList1.removeAll(localArrayList2);
        System.out.println("l1 has extra data:" + localArrayList1);
      }
      else if (!localArrayList1.containsAll(localArrayList2))
      {
        localArrayList2.removeAll(localArrayList1);
        System.out.println("l2 has extra data:" + localArrayList2);
      }
      System.out.println("the result is wrong. please check.");
    }
    localObject2 = localJdbcTemplate1.queryForList("select id from students where value=100 order by id");
    Object localObject3 = localJdbcTemplate2.queryForList("select id from `order` where value=100 order by id");
    localArrayList1.clear();
    localArrayList2.clear();
    Iterator localIterator1 = ((List)localObject2).iterator();
    Iterator localIterator2;
    Object localObject5;
    while (localIterator1.hasNext())
    {
      localObject4 = (Map)localIterator1.next();
      localIterator2 = ((Map)localObject4).values().iterator();
      while (localIterator2.hasNext())
      {
        localObject5 = localIterator2.next();
        localArrayList1.add(localObject5.toString());
      }
    }
    localIterator1 = ((List)localObject3).iterator();
    while (localIterator1.hasNext())
    {
      localObject4 = (Map)localIterator1.next();
      localIterator2 = ((Map)localObject4).values().iterator();
      while (localIterator2.hasNext())
      {
        localObject5 = localIterator2.next();
        localArrayList2.add(localObject5.toString());
      }
    }
    System.out.println("id list on datasource1 which keep old value,size:" + localArrayList1.size() + ", value :" + localArrayList1);
    System.out.println("id list on datasource2 which keep old value,size:" + localArrayList2.size() + ", value :" + localArrayList2);
    if ((localArrayList1.size() == localArrayList2.size()) && (localArrayList1.containsAll(localArrayList2)) && (localArrayList2.containsAll(localArrayList1)))
    {
      System.out.println("the result is good.");
    }
    else
    {
      if (!localArrayList2.containsAll(localArrayList1))
      {
        localArrayList1.removeAll(localArrayList2);
        System.out.println("l1 has extra data:" + localArrayList1);
      }
      else if (!localArrayList1.containsAll(localArrayList2))
      {
        localArrayList2.removeAll(localArrayList1);
        System.out.println("l2 has extra data:" + localArrayList2);
      }
      System.out.println("the result is wrong. please check.");
    }
    int m = localJdbcTemplate1.queryForInt("select sum(score) from persons");
    int n = localJdbcTemplate2.queryForInt("select sum(score) from persons");
    System.out.println("failed transacion number:" + (i * 200 - m) / 2 + "," + (i * 200 - n) / 2);
    localClassPathXmlApplicationContext.close();
    System.exit(0);
  }
}


/* Location:           C:\Users\ibm\Downloads\txc-yun-sample (1)\txc-yun-sample\lib\txc-client-2.0.72.jar
 * Qualified Name:     com.taobao.txc.sample.StandardModeClient2
 * JD-Core Version:    0.7.0.1
 */